/*
 * 自定义序列化对外接口
 */

#pragma once

namespace raft::utils {
    class DataStream;

    class Serialize {
    public:
        virtual void serialize(DataStream &stream) const = 0;

        virtual bool deserialize(DataStream &stream) = 0;
    };

#define SERIALIZE(...)                                      \
    void serialize(raft::utils::DataStream &stream) const { \
        char type = raft::utils::DataStream::CUSTOM;        \
        stream.write((char *) &type, sizeof(char));         \
        stream.writeArgs(__VA_ARGS__);                      \
    }                                                       \
                                                            \
    bool deserialize(raft::utils::DataStream &stream) {     \
        char type;                                          \
        stream.read(&type, sizeof(char));                   \
        if (type != raft::utils::DataStream::CUSTOM) {      \
            return false;                                   \
        }                                                   \
        stream.readArgs(__VA_ARGS__);                       \
        return true;                                        \
    }
}// namespace raft::utils